export default {
  methods: {
    // 根据环境 对应地址
    toAction(action) {
      if (!action) return action;
      if (action.indexOf("http://") === 0 || action.indexOf("https://") === 0)
        return action;

      const prefix = process.env.NODE_ENV == "development" ? "/api/" : "/";
      action = prefix + action;
      action = action.replace("\\", "/").replace("//", "/");
      return action;
    },

    // 例如 从model中获取 model.avatar.url的值
    getValue(model, pathStr) {
      if (!model) return null;
      if (!pathStr) return null;

      const paths = pathStr.split(".");

      let tmp = JSON.parse(JSON.stringify(model));
      for (let index = 0; index < paths.length; index++) {
        if (!tmp) break; // 无值要跳出
        tmp = tmp[paths[index]];
      }
      return tmp;
    }
  }
};
